import Vue from 'vue'
import Vuex from 'vuex'
import {getWebInfo} from '@/api'
import {getTimeInterval} from '@/utils'

Vue.use(Vuex)

// 定时器
let timer = null;

const state = {
    runTimeInterval: '1686106964966',
    onlinePerson: 0,
    runTimeYear: '2023',
    userInfo: JSON.parse(localStorage.getItem('userInfo')),
    // 登录模态框
    loginDialogVisible: false,
    // 网站信息是否初始化赋值
    webInfoFlag: false,
    categoryNum: 0,
    tagNum: 0,
    articleNum: 0,
    webConfig: {
        createTime: '2023-06-01 12:00:00',
        logo: "",
        name: "强の博客",
        recordNum: "蜀ICP备2022016454号-1",
        touristAvatar: "",
        webUrl: "https://www.qiangesoft.cn"
    },
    authConfig: {
        aboutMe: '',
        author: "强哥",
        authorAvatar: "",
        authorInfo: '',
        email: "11111111@qq.com",
    }
}
const mutations = {
    SET_LOGIN_DIALOG: (state, loginDialogVisible) => {
        state.loginDialogVisible = loginDialogVisible;
    },
    SET_USER_INFO: (state, userInfo) => {
        state.userInfo = userInfo;
    },
    SET_WEB_CONFIG: (state, webConfig) => {
        state.webConfig = webConfig;
    },
    SET_AUTH_CONFIG: (state, authConfig) => {
        state.authConfig = authConfig;
    },
    SET_RUNTIME_YEAR: (state, runTimeYear) => {
        state.runTimeYear = runTimeYear;
    },
    SET_ONLINE_PERSON: (state, onlinePerson) => {
        state.onlinePerson = onlinePerson;
    },
    SET_RUNTIME_INTERVAL: (state, runtimeInterval) => {
        clearInterval(timer);
        timer = setInterval(() => {
            state.runTimeInterval = getTimeInterval(runtimeInterval);
        }, 1000);
    }
}
const actions = {
    setLoginDialog: ({commit}, v) => {
        commit('SET_LOGIN_DIALOG', v);
    },
    setUserInfo: ({commit}, v) => {
        commit('SET_USER_INFO', v);
    },
    setWebInfo: ({commit}) => {
        if (!state.webInfoFlag) {
            getWebInfo().then(res => {
                let webConfig = res.data.webConfig;
                commit('SET_WEB_CONFIG', webConfig);
                let authConfig = res.data.authConfig;
                commit('SET_AUTH_CONFIG', authConfig);
                let runTimeInterval = res.data.webConfig.runTimeInterval;
                commit('SET_RUNTIME_INTERVAL', runTimeInterval);
                let runTimeYear = res.data.webConfig.runTimeYear;
                commit('SET_RUNTIME_YEAR', runTimeYear);

                state.categoryNum = res.data.categoryNum;
                state.tagNum = res.data.tagNum;
                state.articleNum = res.data.articleNum;
            })
            state.webInfoFlag = true;
        }
    },
    setOnlinePerson: ({commit}, v) => {
        commit('SET_ONLINE_PERSON', v);
    }
}
const getters = {
    categoryNum: state => state.categoryNum,
    tagNum: state => state.tagNum,
    articleNum: state => state.articleNum,
    loginDialogVisible: state => state.loginDialogVisible,
    runTimeInterval: state => state.runTimeInterval,
    runTimeYear: state => state.runTimeYear,
    onlinePerson: state => state.onlinePerson,
    userInfo: state => state.userInfo,
    webConfig: state => state.webConfig,
    authConfig: state => state.authConfig
}
export default new Vuex.Store({
    state,
    mutations,
    actions,
    modules: {},
    getters
})
